package com.astronomy.recruit.service;

import com.astronomy.entity.PageResult;
import com.astronomy.recruit.mapper.RecruitMapper;
import com.astronomy.recruit.pojo.Recruit;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

@Service
@Transactional
public class RecruitService {

    @Autowired
    private RecruitMapper recruitMapper;

    /**
     * 全部数据
     * @return
     */
    public List<Recruit> findAll(){
        return recruitMapper.selectAll();
    }

    /**
     * 根据ID查询
     * @param id
     * @return
     */
    public Recruit findById(String id){
        return  recruitMapper.selectByPrimaryKey(id);
    }

    /**
     * 增加
     * @param recruit
     */
    public void add(Recruit recruit){
        recruitMapper.insert(recruit);
    }

    /**
     * 修改
     * @param recruit
     */
    public void update(Recruit recruit){
        recruitMapper.updateByPrimaryKey(recruit);
    }

    /**
     * 删除
     * @param id
     */
    public void delete(String id){
        recruitMapper.deleteByPrimaryKey(id);
    }


    /**
     * 分页查询
     * @param page
     * @param size
     * @return
     */
    public Page<Recruit> findPage(int page, int size){
        PageHelper.startPage(page,size);
        return (Page<Recruit>)recruitMapper.selectAll();
    }

    /**
     * 条件查询
     * @param searchMap
     * @return
     */
    public List<Recruit> findList(Map<String, Object> searchMap){
        Example example = createExample(searchMap);
        return recruitMapper.selectByExample(example);
    }

    /**
     * 构建查询对象
     * @param searchMap
     * @return
     */
    private Example createExample(Map<String, Object> searchMap){
        Example example=new Example(Recruit.class);
        Example.Criteria criteria = example.createCriteria();
        if(searchMap!=null){
            // ID
            if(searchMap.get("id")!=null && !"".equals(searchMap.get("id"))){
                criteria.andLike("id","%"+searchMap.get("id")+"%");
           	}
            // 职位名称
            if(searchMap.get("jobname")!=null && !"".equals(searchMap.get("jobname"))){
                criteria.andLike("jobname","%"+searchMap.get("jobname")+"%");
           	}
            // 薪资范围
            if(searchMap.get("salary")!=null && !"".equals(searchMap.get("salary"))){
                criteria.andLike("salary","%"+searchMap.get("salary")+"%");
           	}
            // 经验要求
            if(searchMap.get("condition")!=null && !"".equals(searchMap.get("condition"))){
                criteria.andLike("condition","%"+searchMap.get("condition")+"%");
           	}
            // 学历要求
            if(searchMap.get("education")!=null && !"".equals(searchMap.get("education"))){
                criteria.andLike("education","%"+searchMap.get("education")+"%");
           	}
            // 任职方式
            if(searchMap.get("type")!=null && !"".equals(searchMap.get("type"))){
                criteria.andLike("type","%"+searchMap.get("type")+"%");
           	}
            // 办公地址
            if(searchMap.get("address")!=null && !"".equals(searchMap.get("address"))){
                criteria.andLike("address","%"+searchMap.get("address")+"%");
           	}
            // 企业ID
            if(searchMap.get("eid")!=null && !"".equals(searchMap.get("eid"))){
                criteria.andLike("eid","%"+searchMap.get("eid")+"%");
           	}
            // 状态
            if(searchMap.get("state")!=null && !"".equals(searchMap.get("state"))){
                criteria.andLike("state","%"+searchMap.get("state")+"%");
           	}
            // 网址
            if(searchMap.get("url")!=null && !"".equals(searchMap.get("url"))){
                criteria.andLike("url","%"+searchMap.get("url")+"%");
           	}
            // 标签
            if(searchMap.get("label")!=null && !"".equals(searchMap.get("label"))){
                criteria.andLike("label","%"+searchMap.get("label")+"%");
           	}
            // 职位描述
            if(searchMap.get("content1")!=null && !"".equals(searchMap.get("content1"))){
                criteria.andLike("content1","%"+searchMap.get("content1")+"%");
           	}
            // 职位要求
            if(searchMap.get("content2")!=null && !"".equals(searchMap.get("content2"))){
                criteria.andLike("content2","%"+searchMap.get("content2")+"%");
           	}


        }
        return example;
    }

    /**
     * 条件+分页查询
     * @param searchMap 查询条件
     * @param page 页码
     * @param size 页大小
     * @return 分页结果
     */
    public PageResult<Recruit> findPage(Map<String, Object> searchMap, int page, int size){
        Example example = createExample(searchMap);
        PageHelper.startPage(page,size);
        List<Recruit> recruits = recruitMapper.selectByExample(example);
        PageInfo<Recruit> recruitPageInfo = new PageInfo<>(recruits);
        return new PageResult<>(recruitPageInfo.getTotal(),recruitPageInfo.getList());
    }

    /**
     * 查询推荐职位
     * @param state
     * @return
     */
    public List<Recruit> recommend(String state){
        return recruitMapper.queryByStateOrderByCreatetimeDesc(state);
    }

    /**
     * 最新职位
     * @param state
     * @return
     */
    public List<Recruit> newList(String state){
        return recruitMapper.queryByStateNotOrderByCreatetimeDesc(state);
    }

}
